<?php
/*
Plugin Name: Synergy Admin Menus and Pages
Plugin URI: http://
Description: Plugin is provides menues and pages for the administrator
Author: Maia Engeli
Version: 1.0
Author URI: http://
*/


// MAIN CONFIGURATION MENU
// provide a menu to configure the path to ffmpeg for the assets plugin

wp_enqueue_script('syn_admin_script', '/wp-content/plugins/syn_admin/syn_admin.js');
add_action('admin_menu', 'syn_config_menu');

function syn_config_menu() {
	add_menu_page('Configure Synergy', 'Synergy Configuration', 'manage_options', 
                'syn_configuration', 'syn_configuration');
   // prep the database for the following options
  	add_option("ffmpegPath", '', '', 'yes');
	add_option("assetsDirectory", '', '', 'yes');
		
	// Check for asset directory path
	if(isset($_POST[syn_assetsdirectory]) && $_POST[syn_assetsdirectory] == "Submit") {
		$text = trim($_POST['newAssetsDirectory']);
		if ($text != "") {			
			update_option("assetsDirectory", $text);
		}
 	}
	
	// Check for new ffmeg path
	if(isset($_POST[syn_ffmpegpath]) && $_POST[syn_ffmpegpath] == "Submit") {
		$text = trim($_POST['newffmpegPath']);
		if ($text != "") {
			update_option("ffmpegPath", $text);
		}
 	}
 	
 	// delete selected projects
	if(isset($_POST[btnSynProjDelete]) && $_POST[btnSynProjDelete] == "Delete Selected") {
		delete_projects();				
 	}
 	
    // delete selected categories
	if(isset($_POST[btnSynCatDelete]) && $_POST[btnSynCatDelete] == "Delete Selected") {
		delete_categories();				
 	}
}

function syn_configuration() {
?>

<div class="wrap">
  <h1>Configuration of the Synergy System</h1>
  <h2>Directory for Assets </h2>
  <form id="form1" name="form1" method="post" action="">
    <input name="newAssetsDirectory" type="text" id="newAssetsDirectory" 
    value="<?php echo get_option("assetsDirectory")?>" size="64" />
    <input type="submit" name="syn_assetsdirectory" id="syn_assetsdirectory" value="Submit" />
  </form>
  <p>Enter the full local path to the directory into which the assets should be uploaded. It can be anywhere on the local harddrive. The directory has to exist and be writable by the webserver. Subfolders will be created automatically. </p>
  <p>&nbsp;</p>
  <h2>Path to 'ffmpeg'</h2>
  <form id="form1" name="form1" method="post" action="">
    <input name="newffmpegPath" type="text" id="newffmpegPath" 
    value="<?php echo get_option("ffmpegPath")?>" size="64" />
    <input type="submit" name="syn_ffmpegpath" id="syn_ffmpegpath" value="Submit" />
  </form>
  <p>Enter the path to ffmpeg, for example c:\ffmpeg\bin\ffmpeg or /usr/local/bin/ffmpeg</p>
  <p>ffmpeg is an opensource video conversion tool. It is needed to create the thumbnails for videos that are uploaded. If it is not installed on your system please find the newest version on the internet and install it.</p>
</div>
<?php
}



// PROJECTS
// provide an interface for the administrator to delete projects

add_action('admin_menu', 'syn_projects_menu');

function syn_projects_menu() {
  add_menu_page('Delete Projects', 'Projects', 'manage_options', 'syn_projects', 'syn_delete_projects');
}

function syn_delete_projects() {
  echo '<div class="wrap">';
?>
  	
<h1>PROJECTS</h1>
<p>Select projects for deletion</p>

<form class="search-form" action="" method="POST">
<p class="search-box">
	<label class="screen-reader-text" for="user-search-input">Search Projects:</label>
	<input id="user-search-input" name="projectsearch" value="" type="text">
	<input value="Search Projects" name='btnSynProjSearch' class="button" type="submit">
</p>
</form>

<form class='search-form' action='' method='POST'>
<input value="Delete Selected" name='btnSynProjDelete' id='btnSynProjDelete' onclick='return confirmDelete("project");' class="button" type="submit"><br/>

	
<table class="widefat fixed" cellspacing="0">
<thead>
<tr class="thead">
	<th scope="col" id="cb" class="manage-column column-cb check-column" style=""><input type="checkbox"></th>
	<th scope="col" id="projectname" class="manage-column column-username" style="">Project</th>
	<th scope="col" id="date" class="manage-column column-name" style="">Created</th>
	<th scope="col" id="user" class="manage-column column-email" style="">Manager</th>
	<th scope="col" id="email" class="manage-column column-email" style="">E-mail</th>
	<th scope="col" id="phone" class="manage-column column-role" style="">Phone</th>
</tr>
</thead>

<tfoot>
<tr class="thead">
	<th scope="col" id="cb" class="manage-column column-cb check-column" style=""><input type="checkbox"></th>
	<th scope="col" id="projectname" class="manage-column column-username" style="">Project</th>
	<th scope="col" id="date" class="manage-column column-name" style="">Created</th>
	<th scope="col" id="user" class="manage-column column-email" style="">Manager</th>
	<th scope="col" id="email" class="manage-column column-email" style="">E-mail</th>
	<th scope="col" id="phone" class="manage-column column-role" style="">Phone</th>
</tr>
</tfoot>

<tbody id="users" class="list:user user-list">
<?php 
$projCount = 1;
foreach(display_projects() as $projRow) {
	$user_info = get_userdata($projRow->ID);
	echo "<tr id='project-$projCount' class='alternate'>";
	echo "<th scope='row' class='check-column'><input name='delProjects[]' id='project_$projCount' class='administrator' value='$projRow->PROJ_ID' type='checkbox'></th>";
	echo "<td class='projectname column-username'><strong>$projRow->PROJ_NAME</strong></td>";
	echo "<td class='date column-name'>$projRow->PROJ_DATE</td>";
	echo "<td class='name column-name'>$user_info->first_name $user_info->last_name</td>";
	echo "<td class='email column-email'><a href='mailto:$projRow->user_email' title='e-mail: $projRow->user_email'>$projRow->user_email</a></td>";
	echo "<td class='phone'>$projRow->user_url</td>";
	echo "</tr>";
	$projCount++;
}

?>

</tbody>
</table><br/>

<input value="Delete Selected" name='btnSynProjDelete' id='btnSynProjDelete' onclick='return confirmDelete("project");' class="button" type="submit"><br/>
</form>
<?php
echo '</div>';
}

function display_projects() {
	if(isset($_POST[projectsearch]) && $_POST[projectsearch] != ""){
		return get_search_projects($_POST[projectsearch]);
	}
	else {
		return get_all_projects();
	}	
}
function get_all_projects() {
	global $wpdb;
	$projTable = $wpdb->prefix."projects";
	$userTable = $wpdb->prefix."users";
	$myrows = $wpdb->get_results("SELECT a.PROJ_ID, a.PROJ_NAME, a.PROJ_DATE, a.PROJ_INFO, b.ID, b.display_name, b.user_email, b.user_url FROM $projTable as a, $userTable as b WHERE a.MANAGER_ID = b.ID AND IS_DELETED='0'");	
	return $myrows;
}

function get_search_projects($projName) {
	global $wpdb;
	$projTable = $wpdb->prefix."projects";
	$userTable = $wpdb->prefix."users";
	$myrows = $wpdb->get_results("SELECT a.PROJ_ID, a.PROJ_NAME, a.PROJ_DATE, a.PROJ_INFO, b.ID, b.display_name, b.user_email, b.user_url FROM $projTable as a, $userTable as b WHERE a.MANAGER_ID = b.ID AND IS_DELETED='0' AND a.PROJ_NAME LIKE '$projName%'");	
	return $myrows;
}

function delete_projects() {
	global $wpdb;
	$projTable = $wpdb->prefix."projects";
	$projContTable = $wpdb->prefix."projectcontributors";
	foreach ($_POST[delProjects] as $val) {
		$deleteSchema = "UPDATE $projTable SET IS_DELETED='1' WHERE PROJ_ID = $val";
		$wpdb->query($deleteSchema);
		$deleteSchemaCont = "DELETE FROM $projContTable WHERE PROJ_ID = $val";
		$wpdb->query($deleteSchemaCont);
	}
	
}


// CATEGORIES ///////////////////////////////////////////////////////////////////////////////
// provides a way to remove superfluous categories

// PROJECTS
// provide an interface for the administrator to delete projects

add_action('admin_menu', 'syn_categories_menu');

function syn_categories_menu() {
  add_menu_page('Manage Categories', 'Categories', 'manage_categories', 
                                       'syn_categories', 'syn_manage_categories');
}

function syn_manage_categories() {
  echo '<div class="wrap">';
?>
<h1>CATEGORIES</h1>
<p>Select categories for deletion</p>

<form class="search-form" action="" method="POST">
<p class="search-box">
	<label class="screen-reader-text" for="user-search-input">Search Categories:</label>
	<input id="user-search-input" name="categorysearch" value="" type="text">
	<input value="Search Categories" name='btnSynCatSearch' class="button" type="submit">
</p>
</form>

<form class='search-form' action='' method='POST'>
<input value="Delete Selected" name='btnSynCatDelete' id='btnSynCatDelete' onclick='return confirmDelete("categorie");' class="button" type="submit"><br/>

	
<table class="widefat fixed" cellspacing="0">
<thead>
<tr class="thead">
	<th scope="col" id="cb" class="manage-column column-cb check-column" style=""><input type="checkbox"></th>
	<th scope="col" id="projectname" class="manage-column column-username" style="">Category</th>
	<th scope="col" id="date" class="manage-column column-name" style="">Created On</th>
	<th scope="col" id="user" class="manage-column column-email" style="">Created By</th>
	<th scope="col" id="email" class="manage-column column-email" style="">E-mail</th>
	<th scope="col" id="phone" class="manage-column column-role" style="">Phone</th>
</tr>
</thead>

<tfoot>
<tr class="thead">
	<th scope="col" id="cb" class="manage-column column-cb check-column" style=""><input type="checkbox"></th>
	<th scope="col" id="projectname" class="manage-column column-username" style="">Category</th>
	<th scope="col" id="date" class="manage-column column-name" style="">Created On</th>
	<th scope="col" id="user" class="manage-column column-email" style="">Created By</th>
	<th scope="col" id="email" class="manage-column column-email" style="">E-mail</th>
	<th scope="col" id="phone" class="manage-column column-role" style="">Phone</th>
</tr>
</tfoot>

<tbody id="users" class="list:user user-list">
<?php 
$catCount = 1;
foreach(display_categories() as $keyRow) {
	$user_info = get_userdata($keyRow->ID);
	echo "<tr id='project-$catCount' class='alternate'>";
	echo "<th scope='row' class='check-column'><input name='delCategories[]' id='project_$projCount' class='administrator' value='$keyRow->KEY_ID' type='checkbox'></th>";
	echo "<td class='projectname column-username'><strong>$keyRow->KEYWORD</strong></td>";
	echo "<td class='date column-name'>$keyRow->CREATED_DATE</td>";
	echo "<td class='name column-name'>$user_info->first_name $user_info->last_name</td>";
	echo "<td class='email column-email'><a href='mailto:$keyRow->user_email' title='e-mail: $keyRow->user_email'>$keyRow->user_email</a></td>";
	echo "<td class='phone'>$keyRow->user_url</td>";
	echo "</tr>";
	$catCount++;
}

?>

</tbody>
</table><br/>

<input value="Delete Selected" name='btnSynCatDelete' id='btnSynCatDelete' onclick='return confirmDelete("categorie");' class="button" type="submit"><br/>
</form>

<?php 
  echo '</div>';
}



function display_categories() {
	if(isset($_POST[categorysearch]) && $_POST[categorysearch] != ""){
		return get_search_categories($_POST[categorysearch]);
	}
	else {
		return get_all_categories();
	}	
}
function get_all_categories() {
	global $wpdb;
	$keyTable = $wpdb->prefix."keywords";
	$userTable = $wpdb->prefix."users";
	$myrows = $wpdb->get_results("SELECT a.KEY_ID, a.KEYWORD, a.CREATED_DATE, b.ID, b.display_name, b.user_email, b.user_url FROM $keyTable as a, $userTable as b WHERE a.CREATOR_ID = b.ID");	
	return $myrows;
}

function get_search_categories($keyName) {
	global $wpdb;
	$keyTable = $wpdb->prefix."keywords";
	$userTable = $wpdb->prefix."users";
	$myrows = $wpdb->get_results("SELECT a.KEY_ID, a.KEYWORD, a.CREATED_DATE, b.ID, b.display_name, b.user_email, b.user_url FROM $keyTable as a, $userTable as b WHERE a.CREATOR_ID = b.ID AND a.KEYWORD LIKE '$keyName%'");	
	return $myrows;
}

function delete_categories() {
	global $wpdb;
	$keyTable = $wpdb->prefix."keywords";
	$keyProjTable = $wpdb->prefix."projectkeywords";
	foreach ($_POST[delCategories] as $val) {
		$deleteSchema = "DELETE FROM $keyTable WHERE KEY_ID = $val";
		$wpdb->query($deleteSchema);
		$deleteSchemaProj = "DELETE FROM $keyProjTable WHERE KEY_ID = $val";
		$wpdb->query($deleteSchemaProj);
	}
	
}




// HELP ////////////////////////////////////////////////////////////////////////////////////
// provide an interface to create the help sidebar

add_action('admin_menu', 'syn_help_menu');

function syn_help_menu() {
  add_menu_page('Edit Help', 'Help', 'manage_options', 'syn_help', 'syn_edit_help');
  add_option("ffmpegPath", '', '', 'yes');
  add_option("assetsDirectory", '', '', 'yes');
}

function syn_edit_help() {
	global $wpdb;
	$editId = -1;
	show_input(); // evaluate any $_POST queries that INSERT or UPDATE rules
	echo '<div class="wrap">';
	echo '<h1>HELP</h1>';
	if (isset($_POST['syn_help_edit'])) {
	  
  		$editId = $_POST['syn_help_edit'];
  		$title = $wpdb->get_var("SELECT TITLE FROM {$wpdb->prefix}help WHERE id = $editId;");
  		$text = $wpdb->get_var("SELECT TEXT FROM {$wpdb->prefix}help WHERE id = $editId;");
		// echo $text;
  }
  
  if ($editId == -1) {
  ?>
	<h2>Add a new help topic</h2>
	<form method="post" action="">
	  Title:<br/> <input type="text" name="syn_newhelp_title" id="syn_newhelp_title" style="width:40%"/><br/>	  
	  Text:<br/><textarea name="syn_newhelp" rows="4" id="syn_newhelp" style="width:95%"></textarea><br />
	  <input type='submit' name='syn_help_add' id='addhelp' value='add' />
	</form>
    
   <?php } 
   else {  ?>

	<h2>Edit help</h2>
	<form method="post" action="">
	  <input type="text" name="syn_help_edittitle" id="txtsyn_help_edittitle" style="width:40%" value="<?php echo $title; ?>"/>
	  <textarea name="syn_help_edittext" rows="4" style="width:95%"><?php echo $text; ?></textarea>
	  <input type='hidden' name='syn_help_editid' value=<?php echo $editId; ?> />
 	  <input type='submit' name='syn_help_changetext' value='submit' />
	</form>
   <?php

  }
  show_help();   // Display the current help 
  echo "</div>";  
	
}


function show_input() {
	global $wpdb;	
	// ADD a new help
	if(isset($_POST[syn_help_add]) && $_POST[syn_help_add] == "add") {
		$text = $_POST['syn_newhelp'];
		$title = $_POST['syn_newhelp_title'];
		if (($text != "") && ($title != "")){
			$wpdb->query("INSERT INTO {$wpdb->prefix}help (TITLE, TEXT, DATE)   
						VALUES('$title', '$text', NOW())");
		}
 	}	
	// CHANGE/EDIT a help
	else if(isset($_POST['syn_help_changetext']) && isset($_POST['syn_help_edittitle'])) {
		$id = $_POST['syn_help_editid'];
		$text = $_POST['syn_help_edittext'];
		$title = $_POST['syn_help_edittitle'];
		if (($text != "") && ($title != "")) {
			$wpdb->query("UPDATE {$wpdb->prefix}help 
						  SET TITLE = '$title', TEXT = '$text', DATE = NOW() WHERE id = $id;");
						
		}
	}	
	// DELETE A HELP (javascript has asked for confirmation)
	else if(isset($_POST['syn_help_delete'])) {
		$id = $_POST['syn_help_delete'];
		$wpdb->query("DELETE from {$wpdb->prefix}help WHERE id = $id;");
	}
	// SWAP TWO HELP (javascript has asked for confirmation)
	else if(isset($_POST['syn_help_swap'])) {
		$id = $_POST['syn_help_swap'];
		$id2 = $_POST['syn_help_swap2'];
		$wpdb->query("UPDATE {$wpdb->prefix}help SET id = -1 WHERE id = $id");
		$wpdb->query("UPDATE {$wpdb->prefix}help SET id = $id WHERE id = $id2");
		$wpdb->query("UPDATE {$wpdb->prefix}help SET id = $id2 WHERE id = -1");						
	}	
}


// this displays the existing rules below the edit form
function show_help() {
	global $wpdb;
  
	echo"<p>&nbsp;</p>";
	echo "<h2>Current Help Topics</h2>\n";

	$result = $wpdb->get_results("SELECT id, TITLE, TEXT FROM {$wpdb->prefix}help ORDER BY id"); 
	echo '<form method="post" action="">' . "\n";
	echo "<ol style=\"padding-left:16px\">\n";
	
	$previd = -1;
	$i = 0;
	foreach ($result as $nextr) {
		if ($i != 0) {
			echo "<li style='padding-bottom:6px'><strong>$r->TITLE</strong><br />\n";
			echo "<p>$r->TEXT</p>\n";
			echo "<a  href='javascript: help($r->id, \"syn_help_edit\", 0);' style='font-size:0.9em'>edit</a>\n";
			echo " | <a  href='javascript: help($r->id, \"syn_help_delete\", 0);' style='font-size:0.9em'>delete</a>\n";
			if ($i != 1)  echo " | <a  href='javascript: help($r->id, \"syn_help_swap\", $previd);' style='font-size:0.9em'>move up</a>\n" ;
			if ($i != $count) echo " | <a  href='javascript: help($r->id, \"syn_help_swap\", $nextr->id);' style='font-size:0.9em'>move down </a> </li>\n";
			$previd = $r->id;
		 }
		$r=$nextr;
		$i++;
	}

	echo "<li style='padding-bottom:6px'><strong>$r->TITLE</strong><br />\n";
	echo "<p>$r->TEXT</p>\n";
	echo "<a  href='javascript: help($r->id, \"syn_help_edit\", 0);' style='font-size:0.9em'>edit</a>\n";
	echo " | <a  href='javascript: help($r->id, \"syn_help_delete\", 0);' style='font-size:0.9em'>delete</a>\n";
	if ($i != 1)  echo " | <a  href='javascript: help($r->id, \"syn_help_swap\", $previd);' style='font-size:0.9em'>move up</a>\n" ;

	echo "</ol></form>\n";
	
	// a hidden form to send input via javascript
	echo '</div>';
	echo	'<div style="display: none;">';
	echo	'<form method="post" action="" id="hiddenForm"></form>';
	echo "</div>\n";

}







?>
